package com.zzy.demo.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.List;

import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.zzy.demo.model.School;
import com.zzy.demo.service.SchoolService;


@RestController
@RequestMapping("/")
public class SchoolController {
    @Autowired
    SchoolService schoolService;

	@RequestMapping(value = "/" , method = RequestMethod.GET)
	@ResponseBody
	public String hello() {
		//取得根目录路径  
	     String rootPath=getClass().getResource("/").getFile().toString();
		return "hello cloud:"+rootPath;
	}


    @GetMapping("/schools/maybatis/{id}")
    public School helloSchool1(@PathVariable("id") int id) {
        School school = schoolService.getSchoolById(id);
        System.out.println(school);
        return school;
    }

    @GetMapping("/schools/maybatis-plus/{id}")
    public School helloSchool2(@PathVariable("id") int id) {
        School school = schoolService.getById(id);
        System.out.println(school);
        return school;
    }

    @PostMapping("/schools")
    public Boolean insertSchool(@RequestBody School school) {
        Boolean tag = schoolService.save(school);
        System.out.println(tag);
        return tag;
    }
    
    
    @RequestMapping("/updateById")
    public Boolean updateById(String id, String name) {
    	List<String> idList= Lists.newArrayList(id);
    	LambdaUpdateWrapper<School> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.in(CollectionUtils.isNotEmpty(idList), School::getId, idList);
        lambdaUpdateWrapper.set(null!=name, School::getName, name);
        Boolean tag = schoolService.update(lambdaUpdateWrapper);
        System.out.println(tag);
        return tag;
    }


    @GetMapping ("/schools/page")
    public Page<School> schoolsPage() {
        Page<School> page = schoolService.getByPage();
        System.out.println(page);
        return page;
    }
    
    
    
    @GetMapping ("/schools/nameList")
    public List<String> nameList() {
        QueryWrapper<School> wrapper = Wrappers.query();
        wrapper.select("distinct name").in("id", Lists.newArrayList("1","2","3"));
        List<School> schoolList = schoolService.list(wrapper);
        System.out.println("-----schoolList-------"+ JSON.toJSONString(schoolList));
        List nameList = schoolService.listObjs(wrapper);
        System.out.println("-----nameList-------"+ JSON.toJSONString(nameList));
        return nameList;
    }


}